package com.grsu.phonestation.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.grsu.phonestation.dao.SubscribeDAO;
import com.grsu.phonestation.dao.impl.base.AbstractHibernateDAO;
import com.grsu.phonestation.domain.Client;
import com.grsu.phonestation.domain.Subscribe;

@Repository
public class SubscribeDAOImpl extends AbstractHibernateDAO<Subscribe, Integer>
		implements SubscribeDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Subscribe> getSubscribeServiceClient(Client client) {
		Criteria criteria = getSession().createCriteria(Subscribe.class);
		criteria.add(Restrictions.eq("user", client));
		List<Subscribe> list = criteria.list();

		List<Subscribe> result = new ArrayList<Subscribe>();

		if (list != null && !list.isEmpty()) {
			result.addAll(list);
		}

		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Subscribe> getSubscribeSearchUser(String user){
		
		Criteria criteria = getSession().createCriteria(Subscribe.class,
				"subscribe");

		if (user != null && !user.isEmpty()) {
			criteria.createAlias("subscribe.user", "user");
			criteria.add(Restrictions.or(Restrictions.like("user.firstname",
					user, MatchMode.ANYWHERE), Restrictions.like(
					"user.lastname", user, MatchMode.ANYWHERE)));
		}
		
		List<Subscribe> list = criteria.list();

		List<Subscribe> result = new ArrayList<Subscribe>();

		if (list != null && !list.isEmpty()) {
			result.addAll(list);
		}

		return result;
		
	}

}
